package com.heima.article.interceptor;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.utils.thread.ArticleThreadLocalUtils;
import com.heima.utils.thread.BehaviorThreadLocalUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId=request.getHeader("userId");
        if (StringUtils.isNotBlank(userId)){
            //存入ThreadLocal
          ArticleThreadLocalUtils.set(Integer.valueOf(userId));
        }

        return true;
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        //清理内存
        ArticleThreadLocalUtils.clear();



    }
}
